package com.example.mapper;

import com.example.bean.Book;
import com.example.bean.Collect;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CollectMapper {

    // 查询某个用户收藏的所有书籍
    @Select("SELECT * FROM book WHERE bookId IN (SELECT bookId FROM collect WHERE userId=#{userId} AND collectStatus=1)")
    public List<Book> selectBookByUserId(@Param("userId") int userId);

    // 查询某本书被收藏的数量
    @Select("SELECT COUNT(*) FROM collect WHERE bookId=#{bookId} AND collectStatus=1")
    public int selectBookCollected(@Param("bookId") String bookId);

    @Select("SELECT * FROM collect WHERE userId=#{userId} AND bookId =#{bookId}")
    public Collect judgeUserCollect( @Param("userId") int userId, @Param("bookId") String bookId);

    // 某用户添加对某本书的收藏
    @Insert("INSERT INTO collect(userId, bookId, collectStatus) VALUES(#{userId}, #{bookId}, 1)")
    public int insertCollect( @Param("userId") int userId, @Param("bookId") String bookId);

    // 某用户取消对某本书的收藏
    @Delete("delete  from collect  WHERE userId=#{userId} AND bookId=#{bookId}")
    public int deleteCollect(@Param("userId") int userId, @Param("bookId") String bookId);
}
